package com.ceba.test.modules.regist.dao;

import abc.ID;
import abc.data.DataRow;
import abc.data.DataTable;
import abc.data.Sql;
import abc.data.SqlSessionManager;
import abc.data.store.IStore;
import abc.data.store.StoreManager;
import abc.object.IObject;
import abc.object.XObject;
import com.ceba.test.modules.regist.entity.Enterprise;
import com.ceba.test.modules.regist.entity.EnterpriseRegistLic;
import com.ceba.test.modules.regist.entity.NewEnterprise;

import java.util.Date;
import java.util.List;

public class NewEnterPriseDao {

    /**
     * 增加一條記錄
     * @param newEnterprise
     * @return
     */
    public boolean getAddEnterprise(NewEnterprise newEnterprise,String userId){
        IStore store = StoreManager.create("lic_enterprise");
        IObject object = new XObject("lic_enterprise");
        object.put("ID", ID.getGuid());
        object.put("ENTERPRISE_NAME",newEnterprise.getEnterpriseName());
        object.put("ENTERPRISE_ID",ID.get16bID());
        object.put("UPDATE_TIME",new Date());
        object.setCreateTime();
        object.setCreateUser();
        object.setUpdateUser();
        if(store.insert(object)>0){
            return true;
        }
        return false;
    }


    /**
     *修改
     */
    public boolean updateEnterprise(NewEnterprise newEnterprise,String userId){
        IStore store = StoreManager.create("lic_enterprise");
        XObject object = new XObject("ID",newEnterprise.getId());
        object.put("ENTERPRISE_NAME",newEnterprise.getEnterpriseName());
        object.put("PC_CODES",newEnterprise.getPcCodes());
        object.put("UPDATE_TIME",new Date());
        object.setUpdateUser();
        if(store.update(object)>0){
            return true;
        }
        return false;
    }




    /**
     * 刪除一條記錄
     * @param id
     * @return
     */
    public boolean getDeleteEnterprise(String id){
        Sql sql = new Sql("delete from lic_enterprise where ID='"+id+"'");
        return SqlSessionManager.execute(sql);
    }


    /**
     * 获取单个公司用户信息
     *
     */

    public NewEnterprise getNewEnterprise (String Id) throws  Exception{
        Sql sql = new Sql("select * from lic_enterprise where ID='"+Id+"'");
         DataTable dt = SqlSessionManager.getDataTable(sql);
         if(dt!=null && dt.getRowCount()>0){
             NewEnterprise newEnterprise = new NewEnterprise();
             for( DataRow dr:dt){
                 copyDataTableToNewEnterprise(dr,newEnterprise);
             }

             return  newEnterprise;
         }
     return null;
    }

    public void copyDataTableToNewEnterprise(DataRow dr,NewEnterprise newEnterprise){
        newEnterprise.setId(dr.getString("ID"));
        newEnterprise.setEnterpriseName(dr.getString("ENTERPRISE_NAME"));
        newEnterprise.setEnterpriseId(dr.getString("ENTERPRISE_ID"));
        newEnterprise.setPcCodes(dr.getString("PC_CODES"));

    }

    public boolean queryEnterpriseByName(String enterpriseName){
        Sql sql = new Sql("select * from lic_enterprise where ENTERPRISE_NAME='"+enterpriseName+"'");
        long count = SqlSessionManager.getCount(sql);
        if(count>0){
            return false;
        }else{
            return true;
        }
    }
}
